package org.example.cloud.exception;

import lombok.extern.slf4j.Slf4j;
import org.example.cloud.util.ResultData;
import org.example.cloud.util.ReturnCodeEnum;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 宋孟林
 * @description
 * @date: 2025/4/5 9:06
 */
@RestControllerAdvice
@Slf4j
public class GlobalControllerAdvice {
    @ExceptionHandler(CustomException.class)
    //添加这个注解，代表发生这个异常返回什么类型的状态码。不添加返回200 响应状态：httpStatus是spring官方提供的非常规范的响应结果规范
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResultData<String> handleCustomException(CustomException e){
        log.error("全局异常处理异常处理： {}，处理异常的类型为: {}", e.getMessage(), e.getClass().getName());
        return ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
    }

//    @ExceptionHandler(Exception.class)
//    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
//    public ResultData<?> handleAllException(Exception e){
//        log.error("发生了异常");
//        return ResultData.fail("500", "发生了异常");
//    }
}
